<?php

App::uses('AppController', 'Controller');

/**
 * CorePrivilegesGroupsModules Controller
 *
 * @property CorePrivilegesGroupsModule $CorePrivilegesGroupsModule
 */
class CorePrivilegesGroupsModulesController extends AppController {

    /**
     * index method
     *
     * @return void
     */
    public function index() {
        $this->CorePrivilegesGroupsModule->recursive = 0;
        $this->set('corePrivilegesGroupsModules', $this->paginate());
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        if (!$this->CorePrivilegesGroupsModule->exists($id)) {
            throw new NotFoundException(__('Invalid core privileges groups module'));
        }
        $options = array('conditions' => array('CorePrivilegesGroupsModule.' . $this->CorePrivilegesGroupsModule->primaryKey => $id));
        $this->set('corePrivilegesGroupsModule', $this->CorePrivilegesGroupsModule->find('first', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        if ($this->request->is('post')) {
            $this->CorePrivilegesGroupsModule->create();
            if ($this->CorePrivilegesGroupsModule->save($this->request->data)) {
                $this->Session->setFlash(__('The core privileges groups module has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The core privileges groups module could not be saved. Please, try again.'));
            }
        }
        $coreGroups = $this->CorePrivilegesGroupsModule->CoreGroup->find('list');
        $coreModules = $this->CorePrivilegesGroupsModule->CoreModule->find('list');
        $this->set(compact('coreGroups', 'coreModules'));
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        if (!$this->CorePrivilegesGroupsModule->exists($id)) {
            throw new NotFoundException(__('Invalid core privileges groups module'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->CorePrivilegesGroupsModule->save($this->request->data)) {
                $this->Session->setFlash(__('The core privileges groups module has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The core privileges groups module could not be saved. Please, try again.'));
            }
        } else {
            $options = array('conditions' => array('CorePrivilegesGroupsModule.' . $this->CorePrivilegesGroupsModule->primaryKey => $id));
            $this->request->data = $this->CorePrivilegesGroupsModule->find('first', $options);
        }
        $coreGroups = $this->CorePrivilegesGroupsModule->CoreGroup->find('list');
        $coreModules = $this->CorePrivilegesGroupsModule->CoreModule->find('list');
        $this->set(compact('coreGroups', 'coreModules'));
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @throws MethodNotAllowedException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        $this->CorePrivilegesGroupsModule->id = $id;
        if (!$this->CorePrivilegesGroupsModule->exists()) {
            throw new NotFoundException(__('Invalid core privileges groups module'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->CorePrivilegesGroupsModule->delete()) {
            $this->Session->setFlash(__('Core privileges groups module deleted'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Core privileges groups module was not deleted'));
        $this->redirect(array('action' => 'index'));
    }

    public function getModulesIdFromPrivilegesGroup($groupId) {
        $options = array('conditions' => array('CorePrivilegesGroupsModule.core_groups_id' => $groupId));
        $data = $this->CorePrivilegesGroupsModule->find('all', $options);
        $modules = array();
        foreach ($data as $value) {
            $modules[] = ($value["CorePrivilegesGroupsModule"]["copg_read"]==1?'r':
                $value["CorePrivilegesGroupsModule"]["copg_write"]==1?'w':'a')
            .'_'.intval($value["CorePrivilegesGroupsModule"]["core_modules_id"]);
        }
        return $modules;
    }
    
    public function getPrivilegesFromModuleAndGroup($groupId, $moduleId) {
        $options = array('conditions' => array(
            'CorePrivilegesGroupsModule.core_groups_id' => $groupId,
            'CorePrivilegesGroupsModule.core_modules_id' => $moduleId));
        $data = $this->CorePrivilegesGroupsModule->find('first', $options);
        if(!empty($data["CorePrivilegesGroupsModule"])){
            if($data["CorePrivilegesGroupsModule"]["copg_read"]){
                return 'r';
            }else if($data["CorePrivilegesGroupsModule"]["copg_write"]){
                return 'w';
            }else if($data["CorePrivilegesGroupsModule"]["copg_admin"]){
                return 'a';
            }
        }
        return '';
    }

}
